package com.cmpe451.nutty.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.cmpe451.nutty.model.Follow;
import com.cmpe451.nutty.model.TwitterUser;
import com.cmpe451.nutty.model.User;

@Repository
public class TwitterDaoImpl extends BaseDaoImpl implements TwitterDao, BaseDao {

	@Override
	public int findTwitterUsersCount() {
		Criteria criteria = createCriteria(TwitterUser.class);
		criteria.setProjection(Projections.rowCount());
		return ((Number) criteria.uniqueResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Follow> findFollowedOnes(String username) {
		Criteria criteria = createCriteria(Follow.class);
		criteria.add(Restrictions.eq("name", username));
		return criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Follow> findFollowingOnes(String username) {
		// TODO Auto-generated method stub
		Criteria critera = createCriteria(Follow.class);
		critera.add(Restrictions.eq("follows", username));
		return critera.list();
	}


	@Override
	public Follow findFollowshipByUsernames(String currentUsername,
			String username) {
		Criteria criteria = createCriteria(Follow.class);
		criteria.add(Restrictions.eq("name", currentUsername)).add(Restrictions.eq("follows", username));
		return (Follow)criteria.uniqueResult();
	}

}
